package com.system.outpatientsystem.controller;

import com.system.outpatientsystem.pojo.Result;
import com.system.outpatientsystem.pojo.User;
import com.system.outpatientsystem.pojo.price_table;
import com.system.outpatientsystem.pojo.record_table;
import com.system.outpatientsystem.service.PriceService;
import com.system.outpatientsystem.service.RecordService;
import com.system.outpatientsystem.service.UserService;
import com.system.outpatientsystem.utils.JwtUtils;
import com.system.outpatientsystem.utils.MD5Utils;
import com.system.outpatientsystem.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/price")
public class PriceController {

    @Autowired
    private PriceService priceService;

    @Autowired
    private RecordService recordService;

    @PutMapping("/findByName")
    public Result<Integer> findByName(@RequestBody Map<String,String> ppp){
        String name=ppp.get("name");
        Integer price=priceService.findByName(name);
        return Result.success(price);
    }

    @PutMapping("/sum")
    public Result<Integer> count_sum(@RequestBody Map<String,String> ppp){
        Integer sum=0;
        String str=ppp.get("formulation");
        // 使用split方法将字符串分割成数组
        String[] itemsArray = str.split(",");
        // 将数组转换为List
        List<String> itemsList = Arrays.asList(itemsArray);

        for (String item : itemsList) {
            sum=sum+priceService.findByName(item);
        }

        return Result.success(sum);
    }

    @PutMapping("/pay")
    public Result pay_sum(@RequestBody Map<String,Integer> ppp){
        Integer sum= ppp.get("sum");
        Integer id=ppp.get("id");
        record_table re=recordService.findById(id);
        re.setFormulationPrice(sum);
        recordService.recordUpdate(re);
        return Result.success();
    }

    @PutMapping("/repay")
    public Result repay_sum(@RequestBody Map<String,Integer> ppp){
        Integer sum= ppp.get("sum");
        Integer id=ppp.get("id");
        record_table re=recordService.findById(id);
        re.setPayStatus(sum);
        recordService.recordUpdate(re);
        return Result.success();
    }

}
